package com.takeaway.service.serviceimpl;

import com.takeaway.const_.Const;
import com.takeaway.dao.daointerface.SentCouponDao;
import com.takeaway.dao.daolmpl.SentCouponImpl;
import com.takeaway.dao.daolmpl.ShopDaoImpl;
import com.takeaway.pojo.SentCoupon;
import com.takeaway.pojo.Shop;
import com.takeaway.service.serviceinterface.ShopService;

import java.util.List;

/**
 * @author 后台赖骏
 */
public class ShopServiceImpl implements ShopService {
    private static final ShopDaoImpl SHOP_DAO = new ShopDaoImpl();
    private static final SentCouponDao SENT_COUPON_DAO = new SentCouponImpl();
    @Override
    public List<Shop> showShop(int begin, int listNum) throws Exception {
        return SHOP_DAO.selectShop(begin,listNum);
    }
    @Override
    public long checkShopNum() throws Exception {
        return SHOP_DAO.selectListNum();
    }

    @Override
    public List<Shop> searchShop(String checkFactor) throws Exception {
        return SHOP_DAO.selectLikeMore(Shop.class,checkFactor);
    }

    @Override
    public int addShop(String shopName, int ownerId, String description) throws Exception {
        String[] checkField = {Const.FIELD_BUSINESS_ID};
        Shop shop = SHOP_DAO.selectSingle(new Shop(), checkField, ownerId);
        if (shop.getId()!=0){
            return 0;
        }
        int insert = SHOP_DAO.insert(new Shop(shopName, ownerId, description,0,0));
        shop = SHOP_DAO.selectSingle(new Shop(), checkField, ownerId);
        return SENT_COUPON_DAO.insert(new SentCoupon(0, shop.getId(), 1))+insert;
    }

    @Override
    public int updateShop(String shopName, int ownerId, String description,int isSent) throws Exception {
        String[] checkField1 = {Const.FIELD_BUSINESS_ID};
        String[] checkField2 = {Const.FIELD_SHOP_ID};
        Shop shop = SHOP_DAO.selectSingle(new Shop(), checkField1, ownerId);
        shop.setShopName(shopName);
        shop.setDescription(description);
        SentCoupon sentCoupon = SENT_COUPON_DAO
                .selectSingle(new SentCoupon(), checkField2, shop.getId());
        sentCoupon.setIsSent(isSent);
        SENT_COUPON_DAO.update(sentCoupon);
        return SHOP_DAO.update(shop);

    }

    @Override
    public Object[] checkShop(int ownerId) throws Exception {
        String[] checkField = {Const.FIELD_BUSINESS_ID};
        Shop shop = SHOP_DAO.selectSingle(new Shop(), checkField, ownerId);
        String[] checkField1 = {Const.FIELD_SHOP_ID};
        SentCoupon sentCoupon = SENT_COUPON_DAO.selectSingle(new SentCoupon(), checkField1, shop.getId());
        Object[] objects = new Object[2];
        objects[0] = shop;
        objects[1] = sentCoupon;
        return objects;
    }

    @Override
    public List<Shop> checkManageShop(int shopCode) throws Exception {
       String[] checkField={Const.FIELD_SHOP_CODE};
        return SHOP_DAO.selectMore(Shop.class,checkField,shopCode);
    }

    @Override
    public int updateShopCode(int shopId) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        Shop shop = SHOP_DAO.selectSingle(new Shop(), checkField, shopId);
        shop.setShopCode(Const.SHOP_RUN_CODE);
        return SHOP_DAO.update(shop);
    }

    @Override
    public List<Shop> checkCollect(int userId) throws Exception {
        return SHOP_DAO.selectCollect(userId);
    }

    @Override
    public int updateStar(int shopId) throws Exception {
        String[] checkField = {Const.FIELD_ID};
         SHOP_DAO.selectSingle(new Shop(),checkField,shopId);
        Shop shop = SHOP_DAO.selectSingle(new Shop(), checkField, shopId);
        shop.setStar(shop.getStar()+1);
        return SHOP_DAO.update(shop);
    }
}
